/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package qlvbdq.gui;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Nguyen Vu Huy
 */
public class ConnectDatabase {
    private Connection conn;
    public ConnectDatabase() throws ClassNotFoundException, SQLException{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String url = "jdbc:odbc:driver={SQL Server};Server=HYUBYN\\HYUBYNSERVER;Database=VBDQ;UserName=sa;Password=515154";
        this.conn = DriverManager.getConnection(url);
        
    }
    
    
    public ResultSet getData(String tblName) throws SQLException{
        ResultSet kq = null;
        Statement statement = this.conn.createStatement();
        if(statement != null){
            String sql = "SELECT * FROM " + tblName;
            kq = statement.executeQuery(sql);
        }
        else{
            System.out.print("error");
        }
        return kq;
        
    }
    
    public void Close() throws SQLException{
        if(conn != null)
            conn.close();
    }
    public static void main(String[] args) throws ClassNotFoundException, SQLException{
        ConnectDatabase connect = new ConnectDatabase();
        ResultSet rs =  connect.getData("NGUOIDUNG");
        while(rs.next()){
            System.out.print(rs.getString("USERNAME"));
        }
    }
}
